<template>
	<view class="container poRel">
		<view class="register"></view>
		<view class="login" style="margin-top: 20%;">欢迎登录</view>
		<view class="inputlist">
			<!-- <view class="inputitem">
				<view>
					<image src="/static/imgs/icon/mobile.png"></image>
				</view>
				<input type="tel" v-model="tel" placeholder="请输入手机号" placeholder-style="font-size:14px;" />
			</view>
			<view class="inputitem">
				<view>
					<image src="/static/imgs/icon/pwd.png"></image>
				</view>
				<view class="dis disAl disJuB">
					<input v-model="code" type="number" class="wid70" placeholder="请输入验证码"
						placeholder-style="font-size:14px;" />
					<view v-if="sec == 0" @click="getCheckNum()" class="dis disAl disJuC wid30" style="color: #07b56d;">
						获取验证码</view>
					<view v-if="sec > 0" class="dis disAl disJuC wid30" style="color: #07b56d;">{{sec}}s</view>
				</view>
			</view> -->
			<view class="dis disJuC">
				<image src="../../static/imgs/icon/weixinLogin.png" style="width: 88rpx;height: 88rpx;"></image>
			</view>
			<view class="button borRad20 hei80 dis disAl disJuC" @click.stop="getWeixin()">微信一键登录</view>
		</view>
		<!-- <view class="wid100 poAbs" style="bottom: 150rpx;">
			<view class="wid100 dis disJuC" style="">
				<view style="width: 120rpx;" @click.stop="getWeixin()">
					<view class="dis disJuC">
						<image src="../../static/imgs/icon/weixinLogin.png" style="width: 88rpx;height: 88rpx;"></image>
					</view>
					<view class="textCen foSi25 mar-top20" style="color: #777777;">微信登录</view>
				</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	import {
		User
	} from '../../model/user-model.js';
	var user = new User();
	import { Token } from '../../utils/token.js';
	var token = new Token();
	// pages/login/index.js
	export default {
		data() {
			return {
				tel: '',
				code: '',
				sec: 0
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			uni.setNavigationBarTitle({
				title: '用户登录'
			});
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {
			var option = this.getUrlCode()
			if (option.code) {
				uni.setStorageSync('code', option.code)
				this.getOpenID()
			}
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			toPerRegister: function() {
				uni.navigateTo({
					url: '/pages/register/index'
				});
			},
			checkPhone(phone) {
				if (!(/^1[3456789]\d{9}$/.test(phone))) {
					return false;
				}
				return true;
			},
			getCheckNum() {
				var that = this;
				if (!that.checkPhone(that.tel)) {
					return uni.showToast({
						title: '手机号错误',
						icon: 'none'
					})
				}
				// 初始化定时器
				let t = null;
				// 倒计时1分钟
				this.sec = 60
				that.getCode()
				// 计时器清零
				clearInterval(t);
				// that.sendcode()
				t = setInterval(() => {
					// 当倒计时完时，显示重新发送
					if (this.sec == 0) {
						clearInterval(t);
						return;
					}
					// 实现倒计时效果
					this.sec--;
				}, 1000)
			},
			getCode() {},
			getWeixin(){
				this.getWxCode()
			},
			getOpenID(){
				var that = this
				var params = {
					wx_code: uni.getStorageSync('code'),
					pid:uni.getStorageSync('pid')
				};
				user.getOpenID((data) => {
					console.log(data)
					if (data.code === 0) {
						uni.setStorageSync('token',data.data.token)
						console.log('判断内容',data.data.userinfo.alma_mater)
						if(data.data.userinfo.alma_mater == '' || data.data.userinfo.alma_mater == null){
							console.log('跳转完善信息',data.data.userinfo.alma_mater)
							token.verify(
								uni.redirectTo({
									url: '/pages/bindwx/index'
								})
							);
						}else{
							console.log('跳转个人中心')
							token.verify(
								uni.redirectTo({
									url: '/pages/user/index'
								})
							);
						}
						
					} else {
						uni.showToast({
							title: data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				}, params)
			},
			getWxCode() {
				var that = this
				var params = {};
				user.getCode((data) => {
					console.log(data)
					if (data.code === 0) {
						let appid = data.data; //为测试号id
						let local = window.location.href;
						// 页面A1 首次授权
						window.location.href =
							`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
					} else {
						uni.showToast({
							title: data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				}, params)
			},
			getUrlCode() {
				// 截取url中的code方法
				var url = location.search;
				var theRequest = new Object();
				if (url.indexOf("?") != -1) {
					var str = url.substr(1);
					var strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
					}
				}
				return theRequest;
			},
			bindSave() {
				var that = this
				console.log('占位：函数 bindSave 未声明');
				if (!that.checkPhone(that.tel)) {
					return uni.showToast({
						title: '手机号错误',
						icon: 'none'
					})
				}
				if(!that.code){
					return uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
				}
			}
		}
	};
</script>
<style>
	.container {
		background-color: #fff;
		height: 100vh;
		display: flex;
		flex-direction: column;
	}

	.register {
		width: 700rpx;
		padding-top: 60rpx;
		padding-bottom: 30rpx;
		text-align: right;
		margin-right: 50rpx;
		color: #07b56d;
		font-size: 14px;
	}

	.login {
		width: 700rpx;
		font-size: 24px;
		font-weight: 600;
		margin-left: 30rpx;
	}

	.inputlist {
		display: flex;
		flex-direction: column;

		padding: 50rpx;
	}

	.inputitem {
		display: flex;
		flex-direction: row;
		border-bottom: 1rpx solid #f5f5f5;
		padding: 15rpx 0;
	}

	.inputitem view:nth-child(1) {
		height: 60rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.inputitem image {
		width: 40rpx;
		height: 40rpx;
	}

	.inputitem input {
		width: 500rpx;
		height: 60rpx;
		margin-left: 30rpx;
	}

	.button {
		background: #07b56d;
		color: #fff;
		font-size: 14px;
		margin-top: 60rpx;
	}
</style>